using System.IO;
using UnityEngine;

namespace HEFramework
{
    /// <summary>
    ///
    /// UI设置数据体
    /// 
    /// ----------------------------------------
    /// 作者 hep
    /// 创建时间：2023-01-29 14:38:42
    /// ----------------------------------------
    /// </summary>
    public class UISettingScriptableObject : ScriptableObject
    {
        /// <summary>
        /// 数据体保存路径
        /// </summary>
        private const string uiSettingSavePath = "Assets/Resources";

        /// <summary>
        /// 数据体存储名字
        /// </summary>
        private const string uiSettingSaveFileName = "UISettings";

        /// <summary>
        /// 数据体文件后缀
        /// </summary>
        private const string uiSettingFileExtension = ".asset";

        /// <summary>
        /// 数据体文件保存路径
        /// </summary>
        private const string uiSettingSaveFilePath = uiSettingSavePath + "/" + uiSettingSaveFileName + uiSettingFileExtension;

        /// <summary>
        /// UI配置文件名称
        /// </summary>
        private const string uiConfigFileName = "UIConfig";


        /// <summary>
        /// UI预制体生成路径
        /// </summary>
        [SerializeField] private string uiPrefabBuildPath = "Assets/Resources";

        /// <summary>
        /// UI预制体后缀
        /// </summary>
        [SerializeField] private string uiPrefabExtension = "UI";

        /// <summary>
        /// UI配置文件生成路径
        /// </summary>
        [SerializeField] private string uiConfigFileBuildPath = "Assets";

        /// <summary>
        /// UI画布目标分辨率
        /// </summary>
        [SerializeField] private Vector2 uiCanvasScale = new Vector2(1920, 1080);

        /// <summary>
        /// UI模板文件路径
        /// </summary>
        [SerializeField] private string uiTemplateFilePath;


        private static UISettingScriptableObject instance;

        public static UISettingScriptableObject Instance
        {
            get
            {
                if (instance != null)
                {
                    return instance;
                }

                instance = Resources.Load<UISettingScriptableObject>(uiSettingSaveFileName);
                if (instance != null)
                {
                    return instance;
                }

                instance = CreateInstance<UISettingScriptableObject>();

                instance.ResetAll();

#if UNITY_EDITOR

                if (!Directory.Exists(uiSettingSavePath))
                {
                    Directory.CreateDirectory(uiSettingSavePath);
                    UnityEditor.AssetDatabase.Refresh();
                }

                UnityEditor.AssetDatabase.CreateAsset(instance, uiSettingSaveFilePath);
#endif
                return instance;
            }
        }

        public string UIConfigFileName
        {
            get { return uiConfigFileName; }
        }

        public string UIPrefabBuildPath
        {
            get { return uiPrefabBuildPath; }
            set
            {
                if (uiPrefabBuildPath == value)
                {
                    return;
                }

                uiPrefabBuildPath = value;
                Modified = true;
            }
        }

        public string UIPrefabExtension
        {
            get { return uiPrefabExtension; }
            set
            {
                if (uiPrefabExtension == value)
                {
                    return;
                }

                uiPrefabExtension = value;
                Modified = true;
            }
        }

        public string UIConfigFileBuildPath
        {
            get { return uiConfigFileBuildPath; }
            set
            {
                if (uiConfigFileBuildPath == value)
                {
                    return;
                }

                uiConfigFileBuildPath = value;
                Modified = true;
            }
        }

        public Vector2 UICanvasScale
        {
            get { return uiCanvasScale; }
            set
            {
                if (uiCanvasScale == value)
                {
                    return;
                }

                uiCanvasScale = value;
                Modified = true;
            }
        }

        public string UITemplateFilePath
        {
            get { return uiTemplateFilePath; }
            set
            {
                if (uiTemplateFilePath == value)
                {
                    return;
                }

                uiTemplateFilePath = value;
                Modified = true;
            }
        }


        /// <summary>
        /// 是否储存上一次变更的属性 以便于回退操作
        /// </summary>
        private bool Modified
        {
            set
            {
#if UNITY_EDITOR
                //如果外部传值 就保存上一次数值
                if (value)
                {
                    UnityEditor.EditorUtility.SetDirty(this);
                }
#endif
            }
        }


        public void ResetAll()
        {
            uiPrefabBuildPath = "Assets/Resources";
            uiPrefabExtension = "UI";
            uiConfigFileBuildPath = "Assets";
            uiCanvasScale = new Vector2(1920, 1080);
            uiTemplateFilePath = "";
            Modified = true;
        }
    }
}